<template>
  <div class="app-container org">
    <el-card>
      <el-row>
        <el-col :span="24">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form ref="queryRef" label-width="100" :model="queryParams" @submit.native.prevent>
        <el-row>
          <el-col :span="5">
            <el-form-item label="应用名称：" prop="appName" label-width="80">
              <el-input clearable maxlength="100" placeholder="请输入应用名称" v-model="queryParams.appName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="19">
            <el-row justify="end">
              <el-button type="success" @click="handleQuery">搜索</el-button>
              <el-button class="resetBtn" @click="resetQuery">重置</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <p class="pinformation">应用列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
        <el-row style="padding-bottom: 5px; margin-left: 20px">
          <el-button type="success" link icon="Plus" @click="singleAdd">新增</el-button>
        </el-row>
        <el-row style="padding-bottom: 5px; margin-left: 30px">
          <el-button type="success" link icon="Plus" @click="handleExport">导出Excel</el-button>
        </el-row>
        <el-table ref="mainTableRef" :data="appTable" v-loading="appLoading">
          <el-table-column label="AppID" prop="appId" align="center" />
          <el-table-column label="应用名称" prop="appName" align="center" />
          <el-table-column label="应用描述" prop="appDesc" align="center" />
          <el-table-column label="创建时间" prop="createTime" align="center" />
          <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link type="success" @click="handleVision(scope.row)">版本管理</el-button>
              <el-button link type="success" @click="xqApp(scope.row.id)">编辑</el-button>
              <el-button link type="success" @click="delApp(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>
    </el-card>
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      @close="closedInDialog" v-model="appOpen" :title="appTitle" width="1100">
      <p class="pinformation">基础信息</p>
      <el-form label-width="80" ref="rkOrYyRef" :model="form" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="AppID：" prop="appId">
              <el-input clearable maxlength="100" v-model="form.appId"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应用名称：" prop="appName">
              <el-input clearable maxlength="100" v-model="form.appName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="应用简介：" prop="appContent">
              <el-input clearable maxlength="100" v-model="form.appContent"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应用描述：" prop="appDesc">
              <el-input clearable maxlength="100" v-model="form.appDesc"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <p class="pinformation">图标素材</p>
      <el-form-item label="应用图标：" prop="iconList">
        <fileUpload v-model="form.iconList" :fileType="['png', 'jpg', 'gif']"></fileUpload>
      </el-form-item>
      <el-form-item label="应用截图：" prop="screenshotList">
        <fileUpload v-model="form.screenshotList" :fileType="['png', 'jpg', 'gif']"></fileUpload>
      </el-form-item>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" @click="saveApp">保 存</el-button>
          <el-button type="danger" @click="appOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="App">
import {
  listApp,
  delAppList,
  appViewForm,
  postApp,
  putApp,
} from "@/api/system/app";
const router = useRouter();

const { proxy } = getCurrentInstance();

const appTable = ref([]);
const appOpen = ref(false);
const appLoading = ref(false);
const appTitle = ref("");

const data = reactive({
  form: { screenshotList: [], iconList: [] },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
    appId: [{ required: true, message: "AppID不能为空", trigger: "blur" }],
    appName: [{ required: true, message: "应用名称不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
});
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

const total = ref(0);
const dateRange = ref([]);
function getList() {
  appLoading.value = true;
  listApp(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (res) => {
      appLoading.value = false;
      appTable.value = res.rows;
      total.value = res.total;
    }
  );
}
getList();

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}


function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

function delApp(id) {
  proxy.$modal
    .confirm("是否确认删除该条记录？")
    .then(function () {
      delAppList(id).then((res) => {
        proxy.$modal.msgSuccess("删除成功");
        getList();
      });
    })
    .catch(() => { });
}

function xqApp(id) {
  appViewForm(id).then((res) => {
    form.value = res.data;
    appOpen.value = true;
    appTitle.value = "编辑应用";
  });
}

function closedInDialog() {
  form.value = { screenshotList: [], iconList: [] };
}

function singleAdd() {
  appOpen.value = true;
  appTitle.value = "新增应用";
  form.value = { screenshotList: [], iconList: [] };
}

function saveApp() {
  proxy.$refs["rkOrYyRef"].validate((valid) => {
    if (valid) {
      if (!form.value.id) {
        postApp(form.value).then((res) => {
          appOpen.value = false;
          proxy.$modal.msgSuccess("新增成功");
          getList();
        });
      } else {
        putApp(form.value).then((res) => {
          appOpen.value = false;
          proxy.$modal.msgSuccess("编辑成功");
          getList();
        });
      }
    }
  });
}

function handleVision(row) {
  router.push({
    path: "/system/appVersion",
    query: { id: row.id, appId: row.appId, appName: row.appName },
  });
}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "/system/app/export",
    {
      ...queryParams.value,
    },
    `应用列表_${new Date().getTime()}.xlsx`
  );
}
</script>

<style lang="scss" scoped>
.el-radio {
  margin-right: 5px;
}
</style>
